import {
    Ajax,
    urls
} from './urls'

/**
 * 注册用户接口
 * XMLHttpRequest xhr.abort()
 */
export const userReg = async (userPhone, userName) => {
    let result = await Ajax.post(urls.regUrl, {userPhone, userName}).catch(e => {
        console.log(e)
        return {code: 999, message: 'local error'}
    })
    
    if (!!result) return result.data
}

/**
 * 获取手机验证码，axios的传递参数格式，get 第二个参数为：axiosConfig
 * 常用的用户接口请求回来的数据：data: {userId: xxx, useSex: 1/2}，调用一个通用数据接口：data.sex {1: '男', 2: '女', 3: '未知'}
 */

/**
 * 获取手机验证码
 * @param {String} userPhone 
 */
export const getRegCode = async userPhone => {
    let result = await Ajax.get(urls.regCodeUrl, {params: {userPhone: userPhone.replace(/\s/g, '')}}).catch(e => {
        console.log(e)
        return {code: 999, message: 'local error'}
    })
    
    if (!!result) return result.data
}

/**
 * 登录接口
 * @param {String} userPhone 
 * @param {*} validateCode 
 */
export const loginApi = async (userPhone, validateCode) => {
    let result = await Ajax.post(urls.loginUrl, {userPhone, validateCode}).catch(e => {
        console.log(e)
        return {code: 999, message: 'local error'}
    })
    
    if (!!result) return result.data
}